<template>
  <el-dialog
    :title="!dataForm.uid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户Uid" prop="userUid">
      <el-input v-model="dataForm.userUid" placeholder="用户Uid"></el-input>
    </el-form-item>
    <el-form-item label="第三方唯一标识" prop="accountId">
      <el-input v-model="dataForm.accountId" placeholder="第三方唯一标识"></el-input>
    </el-form-item>
    <el-form-item label="用户名" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="来源" prop="source">
      <el-input v-model="dataForm.source" placeholder="来源"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="第三方账号绑定时间" prop="bindTime">
      <el-input v-model="dataForm.bindTime" placeholder="第三方账号绑定时间"></el-input>
    </el-form-item>
    <el-form-item label="第三方账号解绑时间" prop="unbindTime">
      <el-input v-model="dataForm.unbindTime" placeholder="第三方账号解绑时间"></el-input>
    </el-form-item>
    <el-form-item label="个人头像" prop="avatar">
      <el-input v-model="dataForm.avatar" placeholder="个人头像"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item label="性别(1:男2:女)" prop="gender">
      <el-input v-model="dataForm.gender" placeholder="性别(1:男2:女)"></el-input>
    </el-form-item>
    <el-form-item label="第三方用户昵称" prop="nickName">
      <el-input v-model="dataForm.nickName" placeholder="第三方用户昵称"></el-input>
    </el-form-item>
    <el-form-item label="简介" prop="summary">
      <el-input v-model="dataForm.summary" placeholder="简介"></el-input>
    </el-form-item>
    <el-form-item label="是否删除:0否 1是" prop="isDelete">
      <el-input v-model="dataForm.isDelete" placeholder="是否删除:0否 1是"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          uid: 0,
          userUid: '',
          accountId: '',
          userName: '',
          source: '',
          status: '',
          createTime: '',
          updateTime: '',
          bindTime: '',
          unbindTime: '',
          avatar: '',
          email: '',
          gender: '',
          nickName: '',
          summary: '',
          isDelete: ''
        },
        dataRule: {
          userUid: [
            { required: true, message: '用户Uid不能为空', trigger: 'blur' }
          ],
          accountId: [
            { required: true, message: '第三方唯一标识不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          source: [
            { required: true, message: '来源不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          bindTime: [
            { required: true, message: '第三方账号绑定时间不能为空', trigger: 'blur' }
          ],
          unbindTime: [
            { required: true, message: '第三方账号解绑时间不能为空', trigger: 'blur' }
          ],
          avatar: [
            { required: true, message: '个人头像不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '性别(1:男2:女)不能为空', trigger: 'blur' }
          ],
          nickName: [
            { required: true, message: '第三方用户昵称不能为空', trigger: 'blur' }
          ],
          summary: [
            { required: true, message: '简介不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '是否删除:0否 1是不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.uid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uid) {
            this.$http({
              url: this.$http.adornUrl(`/user/useraccount/info/${this.dataForm.uid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userUid = data.userAccount.userUid
                this.dataForm.accountId = data.userAccount.accountId
                this.dataForm.userName = data.userAccount.userName
                this.dataForm.source = data.userAccount.source
                this.dataForm.status = data.userAccount.status
                this.dataForm.createTime = data.userAccount.createTime
                this.dataForm.updateTime = data.userAccount.updateTime
                this.dataForm.bindTime = data.userAccount.bindTime
                this.dataForm.unbindTime = data.userAccount.unbindTime
                this.dataForm.avatar = data.userAccount.avatar
                this.dataForm.email = data.userAccount.email
                this.dataForm.gender = data.userAccount.gender
                this.dataForm.nickName = data.userAccount.nickName
                this.dataForm.summary = data.userAccount.summary
                this.dataForm.isDelete = data.userAccount.isDelete
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/useraccount/${!this.dataForm.uid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uid': this.dataForm.uid || undefined,
                'userUid': this.dataForm.userUid,
                'accountId': this.dataForm.accountId,
                'userName': this.dataForm.userName,
                'source': this.dataForm.source,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'bindTime': this.dataForm.bindTime,
                'unbindTime': this.dataForm.unbindTime,
                'avatar': this.dataForm.avatar,
                'email': this.dataForm.email,
                'gender': this.dataForm.gender,
                'nickName': this.dataForm.nickName,
                'summary': this.dataForm.summary,
                'isDelete': this.dataForm.isDelete
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
